#!/usr/bin/env ruby

LKP_SRC = ENV['LKP_SRC'] || File.dirname(File.dirname(File.realpath($PROGRAM_NAME)))

require 'time'
require "#{LKP_SRC}/lib/statistics"
require "#{LKP_SRC}/lib/string_ext"
require "#{LKP_SRC}/lib/array_ext"
require "#{LKP_SRC}/lib/tests/stats"

test_name = nil
subtest_name = nil
has_dynamic_subtest = false

def valid_datetime?(datetime_str)
  Time.parse(datetime_str)
rescue StandardError
  false
end

stats = LKP::Stats.new

while (line = $stdin.gets)
  line = line.resolve_invalid_bytes

  case line
  when /^(.*) build\/tests\/([^\s]+)$/
    # 2020-10-27 01:29:45 build/tests/core_getstats
    next unless valid_datetime?($1)

    test_name = $2
  when /^(.*) build\/tests\/([^\s]+) --run-subtest ([^\s]+)$/
    # 2020-11-17 03:02:16 build/tests/fbdev --run-subtest info
    next unless valid_datetime?($1)

    test_name = $2
    subtest_name = $3
  when /^(SUCCESS|FAIL|CRASH)/
    # 1 2020-10-27 01:29:45 build/tests/core_getstats
    # 2 IGT-Version: 1.25-ga15f8da1 (x86_64) (Linux: 5.9.0-wt-17357-g18b04b6d608b x86_64)
    # 3 SUCCESS (0.005s)
    next if test_name.nil?

    stats.add_test_result line, test_name, $1
  when /Starting subtest: (.*)$/
    # Starting subtest: invalid-buffer
    next if test_name.nil?

    subtest_name = $1
    has_dynamic_subtest = false
  when /(^|\[.*\] )Subtest (.+): (SUCCESS|FAIL|CRASH|SKIP)/
    # [thread:4146] Subtest thread-performance-write: CRASH (0.580s)
    #
    # Subtest invalid-buffer: SUCCESS (0.000s)
    # one special case is
    # 953 2020-10-26 01:36:11 build/tests/gem_sync
    # ...
    # 956 Starting subtest: default
    # 957 Starting dynamic subtest: default
    # 958   3666944 cycles: 5.455 us
    # 959 Dynamic subtest default: SUCCESS (20.015s)
    # ...
    # 980 Subtest default: SUCCESS (100.073s)
    # ...
    # 1511 Starting subtest: default
    # 1512 Starting dynamic subtest: rcs0
    # 1513   3674112 cycles: 5.444 us
    # 1514 Dynamic subtest rcs0: SUCCESS (20.010s)
    # ...
    # 1521 Subtest default: SUCCESS (60.036s)
    # above line 953-980 and 1511-1521 could be regarded as two sections for
    # gem_sync.default, they include different dynamic subtests.
    # should ignore the 'Subtest default' results from 980 and 1521 to avoid duplicated stats
    next if test_name.nil? || has_dynamic_subtest

    # for SKIP test, no need start with "Starting subtest:"
    # Starting subtest: pipe-C-256x256-bottom-edge
    # Subtest pipe-C-256x256-bottom-edge: SUCCESS (3.159s)
    # ...
    # Subtest pipe-D-64x64-left-edge: SKIP
    # Subtest pipe-D-64x64-right-edge: SKIP
    # Subtest pipe-D-64x64-top-edge: SKIP
    if subtest_name != $2 && $3 != 'SKIP'
      log_warn "subtest_name [#{subtest_name}] != $2 [#{$2}]"
      exit 1
    end

    if %w(gem_shrink i915_pm_rps).include? test_name
      # some subtests of gem_shrink has duplication issue since v5.11-rc2
      # Subtest execbuf1-oom: CRASH (0.021s)
      #  #1 [killpg+0x40]
      #  #2 [__real_main396+0x837]
      #  #3 [main+0x27]
      #  #4 [__libc_start_main+0xeb]
      #  #5 [_start+0x2a]
      # Subtest execbuf1-oom: CRASH (0.021s)
      #  #4 [__libc_start_main+0xeb]
      #  #5 [_start+0x2a]
      # Subtest execbuf1-oom: CRASH (0.021s)

      # avoid duplication of reset and waitboost
      # 2022-01-27 19:24:07 build/tests/i915_pm_rps --run-subtest reset
      # Subtest reset: FAIL (2.276s)
      # (i915_pm_rps:1591) igt_core-INFO: Subtest reset: FAIL (2.276s)
      # 2022-01-27 19:24:10 build/tests/i915_pm_rps --run-subtest waitboost
      # Subtest waitboost: FAIL (1.258s)
      # (i915_pm_rps:1623) igt_core-INFO: Subtest waitboost: FAIL (1.258s)
      stats.add_test_result line, "#{test_name}.#{$2}", $3 unless stats.key? "#{test_name}.#{$2}"
    else
      stats.add_test_result line, "#{test_name}.#{$2}", $3
    end
  when /^Dynamic subtest (.*): (SUCCESS|FAIL|CRASH)/
    # a 'dynamic subtest' is such like a 2nd level subtest under 'subtest' as below
    # 2607 Starting subtest: exec-single-timeline
    # 2608 Starting dynamic subtest: rcs0
    # 2609 Dynamic subtest rcs0: SUCCESS (1.060s)
    next if test_name.nil? || subtest_name.nil?

    has_dynamic_subtest = true

    stats.add_test_result line, "#{test_name}.#{subtest_name}.dynamic.#{$1}", $2
  when /^LKP SKIP igt@(.*)@(.*)$/
    # LKP SKIP igt@gem_streaming_writes@batch-cpu
    stats.add_test_result line, "#{$1}.#{$2}", 'skip' unless stats.key? "#{$1}.#{$2}"
  end
end

stats.dump
